var appData = getApp().globalData;
Page({
    data:{
        actionSheetHidden:true,
        deviceList:[],
        linked:appData.linked,
        linkstate:appData.linked,
        deviceId:appData.deviceId,
        randtype:'',
        act1:'act',
        act2:'',
        act3:'',
        sexsuit:true,
        sex:'',
        actbg:'act1bg',
        Loading:false,
        nologin:false,
        hidden:true,
        users:[],
        malerank:[],
        totalrank:[],
        femalerank:[],
        name:'',
        myrank:
            {
                ranking:0,
                cost_time:0
            }
    },
    confirm:function(){
        if(this.data.nologin){
            wx.reLaunch({
                url: '../accountlogin/login'
            })
        }
        this.setData({hidden:true})
        
    },
    run:function(){
        wx.navigateTo({url: '../run/run?deviceId='+this.data.deviceId})
    },
    onLoad:function(){
        var self = this;
        self.setData({Loading:true,name:appData.nickName,
            sex:appData.gender})
        getApp().request({
            url:'rangking_list',
            data:{
                "type":0
            },success:function(res){
            if(res.data.flag){
                self.setData({
                    users:res.data.data.list,
                    myrank:res.data.data.my_ranking,
                    totalrank:res.data.data.list,
                    Loading:true
                })
            }else{
                if(res.data.code=="0000"){
                    self.setData({nologin:true})
                }
                self.setData({
                    modalText:res.data.msg,
                    hidden:false
                })
            }
        },
            fail:function(res) {
                self.setData({
                    modalText:'请求失败，请稍后再试',
                    hidden:false,
                })
            },complete:function(){
                self.setData({Loading:false})
            }
        })
    },
    linkHeart:function(){
        var self  =this;
        if(!self.data.linking){
            self.setData({
                linkstate:'搜索中...',
                linking:true
            })
            HW.linkHeart(function(){
                self.foundBlue();
            },function(res){
                console.log(res);
                wx.showModal({
                    title: '提示',
                    content: '请在手机-设置-蓝牙 打开蓝牙',
                    showCancel:false
                })
            });
        }else{
            self.closeblue()
            self.setData({
                linking:false
            })
        }
    },
    foundBlue(){
        let self = this;
        let name;
        HW.foundBlue(this,function(devices){
            let name;
            let deviceId;
            if(getApp().globalData.isIOS){ 
                console.log("isiOS")
                deviceId= devices.devices[0].deviceId;
            }else{ 
                deviceId = devices.deviceId
            }
            if(getApp().globalData.isIOS) name= devices.devices[0].name;
            else name = devices.name
            let deviceList = getApp().globalData.deviceList;
            if(!self.isExsit(deviceList,deviceId))deviceList.push(deviceId);   

            self.setData({
                actionSheetHidden:false,
                deviceList:deviceList
            })
        })
    },
    closeblue(){
        HW.closeblue(this);
    },
    cancelAction(){
        this.setData({
            actionSheetHidden:true
        })
        HW.stopSearch();
    },
    selectDevice(e){
        var index = e.target.dataset.index;
        getApp().globalData.linkstate="打卡成功";
        getApp().globalData.linked=true;
        getApp().globalData.deviceId=this.data.deviceList[index];
        this.setData({
            deviceId:this.data.deviceList[index],
            linked:true,
            linkstate:"打开成功",
            actionSheetHidden:true
        })
        HW.stopSearch();
    }, isExsit(list,item){
        for(let i =0;i<list.length;++i){
            if(list[i]===item)return true;
        }
        return false;
    },
    select:function(event){
        var Data = event.target.dataset;
        var info = this.data;
        switch(Data.index){
            case "1":
                if(!info.totalrank||info.totalrank.length==0)this.selectData(0)
                else this.setData({users:info.totalrank})
                this.setData({act1:'act',act2:'',act3:'',actbg:'act1bg',sexsuit:true})
                break;
            case "2":
                if(!info.malerank||info.malerank.length==0)this.selectData(1);
                else this.setData({users:info.malerank})
                this.setData({act1:'',act2:'act',act3:'',actbg:'act2bg'})
                if(info.sex!=2)this.setData({sexsuit:true})
                else this.setData({sexsuit:false})
                break;
            case "3":
                if(!info.femalerank||info.femalerank.length==0)this.selectData(2);
                else this.setData({users:info.femalerank})
                this.setData({act1:'',act2:'',act3:'act',actbg:'act3bg'})
                if(info.sex==2)this.setData({sexsuit:true})
                else this.setData({sexsuit:false})
                break;
        }
    },selectData(type){
        let self = this;
        self.setData({Loading:true})
        getApp().request({
            url: 'rangking_list',
            data: {
                type:type
            },
            success: function(res){
                if(res.data.flag){
                    if(type==0) self.setData({users:res.data.data.list,totalrank:res.data.data.list})
                    else if(type==1) self.setData({malerank:res.data.data.list,users:res.data.data.list})
                    else self.setData({femalerank:res.data.data.list,users:res.data.data.list})
                }else{
                    if(res.data.code=="0000"){
                        self.setData({nologin:true})
                    }
                    self.setData({
                        modalText:res.data.msg,
                        hidden:false
                    })
                }
            },
            fail: function(res) {
                self.setData({
                    modalText:'请求失败，请稍后再试',
                    hidden:false,
                })
            },complete:function(){
                self.setData({Loading:false})
            }
        })
    },
    zan(){
        var Data = event.target.dataset;
        var id = this.id;
        self.setData({Loading:true})
        getApp().request({
            url:'zan',
            data:{
                id:id,
                user_id:appData.UserData.id
            },success(res){
                if(res.data.flag){

                }else{
                    self.setData({
                        modalText:res.data.msg,
                        hidden:false,
                    })
                }
            },fail(res){
                self.setData({
                    modalText:'请求失败，请稍后再试',
                    hidden:false,
                })
            },complete(){
                self.setData({Loading:false})
            }
        })
    }
})